package Servlet;

import moudle.Student;
import service.impl.StudentImplService;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/Do")
public class MyServlet extends MyBaseServlet {
    StudentImplService studentImplService = new StudentImplService();
    /**
     * 逻辑1：渲染数据
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void selectDo(ServletRequest req, ServletResponse resp) throws ServletException, IOException {
        HttpServletRequest req1 = (HttpServletRequest) req;
        HttpSession session = req1.getSession(false);
        HttpServletResponse resp1 = (HttpServletResponse) resp;
        List<Student> allData = studentImplService.getAllService();
        req1.setAttribute("data", allData);
        req1.getRequestDispatcher("ShowList1.jsp").forward(req1, resp1);

    }

    /**
     * 逻辑2：登录
     * @param servletRequest
     * @param servletResponse
     * @throws ServletException
     * @throws IOException
     */
    public void isAdminDo(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        String admin = servletRequest.getParameter("admin");
        String password = servletRequest.getParameter("password");
        String yz = servletRequest.getParameter("yz");//用户输入的验证码

        PrintWriter writer = servletResponse.getWriter();
        HttpServletRequest servletRequest1 = (HttpServletRequest) servletRequest;
        HttpSession session = servletRequest1.getSession(false);
        if(session!=null && session.getAttribute("yanzheng")!=null && !session.getAttribute("yanzheng").equals(yz)){
            writer.write("<script>"+
                    "alert('验证码错误');"+
                    "window.location.href='my.html'</script>");
            return;
        }
//        访问数据访问层
        StudentImplService studentImplService = new StudentImplService();
        Boolean login = studentImplService.isAdminService(admin, password);
        System.out.println("login"+login);
        if(login){
            HttpSession session1 = servletRequest1.getSession(false);
            String id = session1.getId();
            //sessionID保存在客户端
            Cookie cookie = new Cookie("sessionID", id);
            cookie.setMaxAge(60*60*24);
            session.setAttribute("pass","ok");
            HttpServletResponse servletResponse1 = (HttpServletResponse) servletResponse;
            servletResponse1.addCookie(cookie);


            /*List<Student> allData = studentImplService.getAllService();*/
        //    servletRequest1.setAttribute("data", allData);
            servletRequest1.getRequestDispatcher("ShowList1.jsp").forward(servletRequest1, servletResponse1);


        }else{
            HttpServletResponse servletResponse1 = (HttpServletResponse) servletResponse;
            servletResponse1.sendRedirect("error.html");
        }
    }

    /**
     * 逻辑3；删除
     * @param req
     * @param res
     * @throws ServletException
     * @throws IOException
     */
    public void delDo(ServletRequest req, ServletResponse res) throws ServletException, IOException {
//        获取删除的那条记录的主键ID
        String id = req.getParameter("id");
        System.out.println("id = " + id);
//        数据访问层
        Integer idd = Integer.valueOf(id);
        int i = studentImplService.delService(idd);
        System.out.println("i = " + i);
        if(i>0){
//            req.getRequestDispatcher("/Do?method=selectDo").forward(req, res);
            HttpServletResponse servletResponse1 = (HttpServletResponse) res;
            servletResponse1.sendRedirect("Do?method=selectDo");
        }else{
            System.out.println("删除失败！");
        }
    }

    /**
     * 逻辑4：添加
     * @param servletRequest
     * @param servletResponse
     * @throws ServletException
     * @throws IOException
     */
    public void addDo(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        HttpServletRequest req1 = (HttpServletRequest) servletRequest;

        HttpSession session = req1.getSession(false);
        PrintWriter writer = servletResponse.getWriter();
//        短路逻辑，&&一假则为假，后面就不会继续判断了，而||一真则为真，当第一个为假时，后面还会继续判断，知道有一个真的
/*        if(session!=null && session.getAttribute("pass")!=null && session.getAttribute("pass").equals("ok")){
//            正确的业务逻辑
        }else{
            writer.write("<script>"+
                    "alert('请正常登录');"+
                    "window.location.href='my.html'</script>");
            return;
        }*/

        String name = servletRequest.getParameter("name");
        String sex = servletRequest.getParameter("sex");
        String age = servletRequest.getParameter("age");
        Student student = new Student(null,name,sex,age);
        System.out.println("student = " + student);
        int add = studentImplService.insertService(student);
        if(add>0){
//            servletRequest.getRequestDispatcher("Do?method=selectDo").forward(servletRequest, servletResponse);
            HttpServletResponse servletResponse1 = (HttpServletResponse) servletResponse;
            servletResponse1.sendRedirect("Do?method=selectDo");
        }else{
            System.out.println("添加失败！");
        }
    }

    /**
     * 逻辑5：获取一条数据
     * @param req
     * @param res
     * @throws ServletException
     * @throws IOException
     */
    public void getOneDo(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        Integer id = Integer.valueOf(req.getParameter("id"));
        Student student = studentImplService.getOneService(id);
        PrintWriter writer = res.getWriter();
        writer.write("<!doctype html>");
        writer.write("<html>");
        writer.write("<head><title>员工信息修改</title><meta charset='utf-8'></head>");
        writer.write("<body style='background:lightblue;'>");
        writer.write("<form action='/javawebstudy2025_war_exploded/Do?method=updateDo' method='post'>");
        writer.write("<h1>员工信息修改</h1>");
        writer.write("<input type='hidden' value='"+student.getId()+"' name='id'><br>");//隐藏
        writer.write("姓名:<input type='text' value='"+student.getName()+"' name='name'><br>");
        writer.write("性别:<input type='text' value='"+student.getSex()+"' name='sex'><br>");
        writer.write("年龄:<input type='text' value='"+student.getAge()+"' name='age'><br>");
        writer.write("<input type='submit' value='确认更新'>");
        writer.write("</form>");
        writer.write("</body>");
        writer.write("</html>");
    }
    /**
     * 逻辑6：真正实现更新
     * @param req
     * @param res
     * @throws ServletException
     * @throws IOException
     */
    public void updateDo(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        Integer id = Integer.valueOf(req.getParameter("id"));
        String name = req.getParameter("name");
        String sex = req.getParameter("sex");
        String age = req.getParameter("age");
        Student student = new Student(id,name,sex,age);

        int update = studentImplService.updateService(student);
        if(update>0){
//            req.getRequestDispatcher("/Do?method=selectDo").forward(req, res);
            HttpServletResponse servletResponse1 = (HttpServletResponse) res;
            servletResponse1.sendRedirect("Do?method=selectDo");
        }else{
            System.out.println("更新失败！");
        }
    }
}



